<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>赠送商品列表</title>
	<meta name="decorator" content="default"/>
	<style type="text/css">
		.table td {
			vertical-align: middle;
		}
		.footer {
			position:fixed; bottom:0; background:#ABABAB; width:100%; height:35px; line-height:32px; z-index:9999; opacity:1;
			filter:alpha(opacity=60); _bottom:auto; _width:100%; _position:absolute;text-align: right;
			_top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-
			(parseInt(this.currentStyle.marginTop, 10)||0)-(parseInt(this.currentStyle.marginBottom, 10)||0)));
	    }
	</style>
	<script type="text/javascript">
		$(document).ready(function() {
			if('${close}'!='' && '${close}'=='true'){
				top.$.jBox.close();
			}
			var nodeLen = $(".jbox-button-panel", top.document).length;
			$(".jbox-button-panel", top.document).eq(nodeLen-1).remove();
			$("#listForm").validate({
				submitHandler: function(form){
					if($(".activityProducts").length==0){
						showJBoxMess("没有可保存的数据.","error");
						return;
					}
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
		});
		function selectAll(){
			$("input[name='actProductId_checkbox']").each(function(){
				$(this).attr("checked", $("#select_all").is(':checked'));
			});
		}
        //检测商品编号
        function checkProductCode(){
        	var obj = $("#code");
        	if($("#code").val()==""){
				showJBoxMess("请输入商品编号或名称.","error");
				return;
			}
			if($(obj).val()!=""){
				//异步保存活动
				$.ajax({
					type: 'post',
					url: '${ctx}/ivm/inventory/checkProductCode',
					data: 'merchantId=${activity.merchant.id}&code='+$(obj).val(),
					dataType: 'json',
					success: function(data){
						if(data.status==200){
							var product = data.result;
							var isExists = false;
				        	$("input[id='productCode']").each(function(){
				        		if($(this).val()==product.code){
				        			showJBoxMess("商品"+$(obj).val()+"已存在，请重新输入！","error");
				        			isExists=true;
				        			return false;
				        		}
				        	});
				        	if(isExists) return;
							createActivityProduct();
						}else{
							showJBoxMess(data.message,"error");
						}
					},
					failure: function(){
						showJBoxMess("操作失败","error");
					}				
				});
			}
		}
		
		function createActivityProduct(){
			loading("正在提交，请稍等...");
			//异步手动生成商品采购清单
			$.ajax({
				type: 'post',
				url: '${ctx}/ivm/business/discountActivity/productList/create',
				data: 'merchantId=${activity.merchant.id}&activityId=${activity.id }&timeId=${activityTime.id}&code='+$("#code").val(),
				dataType: 'json',
				success: function(data){
					if(data.status==200){
						showJBoxMess(data.message,"success");
						document.location="${ctx}/ivm/business/discountActivity/productList?merchantId=${activity.merchant.id}&activityId=${activity.id }&timeId=${activityTime.id}";
					}else{
						showJBoxMess(data.message,"error");
					}
				},
				failure: function(){
					showJBoxMess("操作失败","error");
				}				
			});
		}
	</script>
</head>
<body>
<form:form id="listForm" modelAttribute="activityTime" action="${ctx}/ivm/business/discountActivity/productList/save" method="post">
	<tags:message content="${message}"/>
	<input type="hidden" id="merchantId" name="merchantId" value="${activity.merchant.id }" />
	<input type="hidden" id="activityId" name="activityId" value="${activity.id }" />
	<form:hidden path="id"/>
	<div style="margin:10px;">
		<div class="breadcrumb form-search">
			<input type="text" id="code" name="code" htmlEscape="false" maxlength="200" class="input-small" style="margin-bottom: 0px; width: 180px;" placeholder="请输入商品编号或名称"/>
			<input class="btn btn-primary" type="button" onclick="checkProductCode()" value="添加赠送商品"/>
		</div>
		<table id="contentTable" class="table table-bordered table-condensed">
			<thead>
				<tr>
					<th width="80"><input type="checkbox" id="select_all" name="select_all" value="全选" onclick="selectAll();"/>全选</th>
					<th width="95">商品封面</th>
					<th>商品编号</th>
					<th>商品名称</th>
					<th>价格</th>
					<th width="105">赠送数量</th>
					<th width="105" style="text-align:center;">排序号</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody>
			<c:forEach items="${activityTime.productList}" var="activityProduct" varStatus="vs">
				<tr class="activityProducts">
					<td style="text-align: center;">
						<input type="hidden" id="actProductId" name="productList[${vs.index }].id" value="${activityProduct.id }"/>
						<input type="hidden" id="productId" name="productList[${vs.index }].product.id" value="${activityProduct.product.id }"/>
						<input type="hidden" id="productCode" value="${activityProduct.product.code }"/>
						<input type="checkbox" id="actProductId_checkbox" value="${activityProduct.id }" />
					</td>
					<td><img id="photo" src="${ctx}/download?filePath=${activityProduct.product.logoUrl }" style="width:95px; height:67px;" /></td>
					<td>${activityProduct.product.code }</td>
					<td>${activityProduct.product.name }</td>
					<td>${activityProduct.product.price}元</td>
					<td style="text-align:center;">
						<input type="text" id="number" name="productList[${vs.index }].number" value="${activityProduct.number }" maxlength="20" min="1" class="required digits" onchange="updateNum(this, 0)" style="width:50px;margin:0;padding:0;text-align:center;"/>件
					</td>
					<td style="text-align:center;">
						<input type="text" id="sort" name="productList[${vs.index }].sort" value="${activityProduct.sort}" style="width:50px;margin:0;padding:0;text-align:center;" min="1" class="required digits">
					</td>
					<td>
						<a href="${ctx }/ivm/business/discountActivity/productList/delete?id=${activityProduct.id }&merchantId=${activity.merchant.id}&activityId=${activity.id}&timeId=${activityTime.id}" onclick="return confirmx('确认要删除该赠送商品吗？', this.href)">删除</a>
					</td>
				</tr>
			</c:forEach>
			</tbody>
		</table>
	</div>
</form:form>
<!--endprint-->
<div class="footer" id="printDiv">
	<input value="保存并关闭" type="button" class="btn btn-primary" style="margin-right: 20px;" onclick="$('#listForm').submit();"/>
</div>
</body>
</html>